package com.xinyue.auto.reader.process;

import com.xinyue.auto.annotation.SetBoolean;
import com.xinyue.auto.exception.EntityException;
import com.xinyue.auto.reader.Process;
import com.xinyue.auto.reader.ProcessContext;
import com.xinyue.auto.util.StringUtil;

public class EntityInitProcess extends ProcessContext{
	public EntityInitProcess(Process read) {
		super(read);
	}
	
	private String name;//entity name
	private String table;//表名称
	private String navigate;//导航标志
	@SetBoolean(false)
	private boolean service;//是否创建service文件
	@SetBoolean(false)
	private boolean controller;//是否创建controller文件

	@Override
	protected void validate() throws Exception {
		name = xml.attributeValue(name_);
		table = xml.attributeValue(table_);
		navigate = xml.attributeValue(navigate_);
		String ser = xml.attributeValue(service_);
		String con = xml.attributeValue(controller_);
		
		if(StringUtil.isNull(name)) {
			throw new EntityException("未定义name属性或其值为空", holder.getException());
		}
		if(StringUtil.isNull(table)) {
			table = name;
		}
		if(!StringUtil.isNull(ser)) {
			service = Boolean.valueOf(ser);
		}
		if(!StringUtil.isNull(con)) {
			controller = Boolean.valueOf(con);
		}
		
		//设置异常信息中的entity名称
		holder.getException().setEntityName(name);
	}

	@Override
	protected void install() {
		//entity类型：1
		holder.getEntity().setType(entityType);
		
		holder.getEntity().setName(name);
		holder.getEntity().setTable(table);
		holder.getEntity().setNavigate(navigate);
		holder.getEntity().setNewService(service);
		holder.getEntity().setNewController(controller);
	}
}
